//
//  UserAccountViewModel.swift
//  weibo19
//
//  Created by Arvin on 15/11/10.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    // 用户access_token状态
    var userLogin: Bool {
        return account?.access_token != nil
    }
    
    var iconURL:NSURL? {
        let url = NSURL(string: account?.avatar_large ?? "")
        return url
    }
    
    var account: UserAccount?
    
    let client_id = "2921022464" // App Key
    let redirect_uri = "http://www.baidu.com"
    let client_secret = "cbb13adcc4aca09ebf155b82e1a47127" // App Secret
    
    override init() {
         super.init()
        account = UserAccount.loadAccount()
    }
    
    // 获取access_token
    func loadAccessToken(code: String, finished: (error: NSError?) ->()) {
        let parma = ["client_id":client_id, "client_secret":client_secret,"grant_type":"authorization_code","code":code, "redirect_uri":redirect_uri]
        let urlStr = "oauth2/access_token"
        NetworkTools.sharedTools.requestJSONData(.POST, urlSting: urlStr, parma: parma) { (result, error) -> () in
            if error != nil {
                finished(error: nil)
                return
            }
            let account = UserAccount(dict: result!)
            // 请求用户信息
            self.loadUserInfo(account, finished: finished)
        }
        
//        let AFN = AFHTTPSessionManager()
        // 给AFN添加解析类型
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        // 查看服务器返回的数据类型 把上面的注释调使用下面方法AFN不进行解析, 返回的是二进制数据
        //        AFN.responseSerializer = AFHTTPResponseSerializer()
//        AFN.POST("https://api.weibo.com/oauth2/access_token", parameters: parma, success: { (_, result) -> Void in
//            //            let str = NSString(data: result as! NSData, encoding: NSUTF8StringEncoding)
//            // 判断result是否有值
//            if let dict = result as? [String: AnyObject] {
//                let account = UserAccount(dict: dict)
//                
//                // 请求用户信息
//                self.loadUserInfo(account, finished: finished)
//            }
//            
//            }) { (_, error) -> Void in
//                
//            }
    }
    // 请求用户信息
    func loadUserInfo(userAcco: UserAccount, finished: (error: NSError?) -> ()) {
        let parma = ["access_token":userAcco.access_token!, "uid":userAcco.uid!]
        let urlString = "2/users/show.json"

        NetworkTools.sharedTools.requestJSONData(.GET, urlSting: urlString, parma: parma) { (result, error) -> () in
            if error != nil {
                finished(error: error)
                return
            }
                userAcco.avatar_large = result?["avatar_large"] as? String
                userAcco.name = result?["name"] as? String
                // 保存用户信息
                userAcco.saveAccount()
                finished(error: nil)
        }
        
//        let AFN = AFHTTPSessionManager()
//        AFN.GET( "https://api.weibo.com/2/users/show.json", parameters: parma, success: { (_, result) -> Void in
//            if let dict = result as? [String: AnyObject] {
//                userAcco.avatar_large = dict["avatar_large"] as? String
//                userAcco.name = dict["name"] as? String
//                // 保存用户信息
//                userAcco.saveAccount()
//                finished(error: nil)
//            }
//            }) { (_, error) -> Void in
//                finished(error: error)
//            }
    }
}
